<template>
  <div class="modal">
    <el-dialog v-model="dialogVisible" :title="isNewRef?'新建用户':'编辑用户'" width="30%" center>
      <div class="form">
        <el-form v-model="formDate" label-width="100px">
          <el-form-item label="用户名" prop="name" size="large">
            <el-input
              v-model="formDate.name"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item label="真实姓名" prop="realname" size="large">
            <el-input
              v-model="formDate.realname"
              placeholder="请输入真实姓名"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="密码"
            prop="password"
            size="large"
            v-if="isNewRef"
          >
            <el-input
              v-model="formDate.password"
              placeholder="请输入密码"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item label="电话号码" prop="cellphone" size="large">
            <el-input
              v-model="formDate.cellphone"
              placeholder="请输入电话号码"
            ></el-input>
          </el-form-item>
          <!-- 选择角色 -->
          <el-form-item label="选择角色" prop="roleId" size="large">
            <el-select
              v-model="formDate.roleId"
              placeholder="请选择角色"
              style="width: 100%"
            >
              <template v-for="item in entireRoles" :key="item.id">
                <el-option :label="item.name" :value="item.id" />
              </template>
            </el-select>
            <!--  选择部门-->
          </el-form-item>
          <el-form-item label="选择部门" prop="departmentId" size="large">
            <el-select
              v-model="formDate.departmentId"
              placeholder="请选择部门"
              style="width: 100%"
            >
              <template v-for="item in entireDepartments" :key="item.id">
                <el-option :label="item.name" :value="item.id" />
              </template>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancelClick">取消</el-button>
          <el-button type="primary" @click="handleConfirm"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { storeToRefs } from "pinia"
import useMainStore from "@/stores/main/main"
import useSystemStore from "@/stores/main/system/system"
import { reactive, ref } from "vue"
const dialogVisible = ref(false)
const mainStore = useMainStore()
const systemStore = useSystemStore()
// 判断编辑不需要密码
const isNewRef = ref<boolean>()
// 记录存储编辑用户数据
const editData = ref()
// mainStore.fetchEntireDataAction()
// 获取角色列表
const { entireRoles, entireDepartments } = storeToRefs(mainStore)
const formDate = reactive<any>({
  name: "",
  realname: "",
  password: "",
  cellphone: "",
  departmentId: "",
  roleId: ""
})
function setModalVisible(isNew: boolean, itemData?: any) {
  dialogVisible.value = true
  isNewRef.value = isNew
  if (!isNew && itemData) {
    // console.log("哈哈哈")
    for (const key in itemData) {
      formDate[key] = itemData[key]
    }
    editData.value = itemData
  } else {
    for (const key in formDate) {
      formDate[key] = ""
    }
    editData.value = null
  }
}
// 确定添加数据
function handleConfirm() {
  dialogVisible.value = false
  // console.log(editData.value)
  // console.log(isNewRef)
  if (!isNewRef.value && editData.value) {
    console.log("编辑")
    systemStore.editUserDataAction(editData.value.id, formDate)
  } else {
    console.log("添加")
    systemStore.newUserDataAction(formDate)
  }
}
// 取消按钮事件
function cancelClick() {
  dialogVisible.value = false
}

defineExpose({
  setModalVisible,
  handleConfirm
})
</script>
<style scoped lang="less">
.modal {
}
.form {
  padding: 0 20px;
}
</style>
